package com.example.administrator.im.ui.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.example.administrator.im.MyApplication;
import com.example.administrator.im.R;
import com.example.administrator.im.config.PhoneParam;

/**
 * create:2018/9/22 9:44
 * author: 姚鸿杰
 **/
public class MySlideFinishActivity extends AppCompatActivity {


    private View decorView; //activity
    private float downX; //手指按下时的未知
    private float screenWidth;    //屏幕宽高
    private final long animTime = 300;  //动画执行时间
    private boolean isSlide = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //禁止横屏
        //Manifest.xml文件中为所有Activity加上配置属性 android:screenOrientation="portrait"
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // 获得decorView
        decorView = getWindow().getDecorView();
        //获取屏幕宽高
        screenWidth = PhoneParam.getInstance(this).getS_screen_width();
    }

    /**
     * 通过重写该方法，对触摸事件进行处理
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            downX = event.getX();   //点击时
            isSlide = !(downX >= 30);
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) { //每次滑动
            float moveDistanceX = event.getX() - downX;

            if (!isSlide) return true;
            if (moveDistanceX < 0)  //表示向左滑动，左滑时超出屏幕时则执行
                moveDistanceX = 0;
            decorView.setX(moveDistanceX);

        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            float moveDistanceX = event.getX() - downX;

            if (!isSlide) return true;

            if (moveDistanceX < 0)  //表示向左滑动，左滑时超出屏幕时则执行
                moveDistanceX = 0;
            if (moveDistanceX > screenWidth / 4) {
                // 如果滑动的距离超过了手机屏幕的 1/3, 滑动处屏幕后再结束当前Activity
                continueMove(moveDistanceX);
            } else {
                // 如果滑动距离没有超过一半, 往回滑动
                rebackToLeft(moveDistanceX);
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 从当前位置一直往右滑动到消失。
     * 这里使用了属性动画。
     */
    private void continueMove(float moveDistanceX) {
        // 从当前位置移动到右侧。
        ValueAnimator anim = ValueAnimator.ofFloat(moveDistanceX, screenWidth);
        anim.setDuration(animTime); // 一秒的时间结束, 为了简单这里固定为300毫秒
        anim.start();

        //滑动监听
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 位移
                float x = (float) (animation.getAnimatedValue());
                decorView.setX(x);
            }
        });

        // 动画结束时结束当前Activity
        anim.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                finish();
            }
        });
    }

    /**
     * Activity被滑动到中途时，滑回去~
     */
    private void rebackToLeft(float moveDistanceX) {
        ObjectAnimator.ofFloat(decorView, "X", moveDistanceX, 0).setDuration(animTime).start();
    }


}
